package com.example.securitydemo.rest;

import com.example.securitydemo.config.AppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class UserResource {

    @Autowired
    private AppProperties appProperties;

    @GetMapping("/greeting")
    public String greeting(){
        return "greeting";
    }

    @GetMapping("/principal")
    public String getPrincipal() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    @GetMapping("/authentication")
    public Authentication getPrincipal(Authentication authentication) {
        return authentication;
    }

    @GetMapping("/users/{username}")
    public String getUsername(@PathVariable String username){
        return username;
    }
}
